package CutLoad.mc
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * 把很多按钮叠在一起。点击进行切换
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class ClickSwapShowSomeBtnOne
	{
		private var arrBtn:Array;
		private var click:Function;
		private var currentIndex:Number;
		//里面各个元素是否是mcToBtn实例
		private var arrBtnIsMcToBtn:Boolean;
		/**
		 * 初始化 
		 * @param arrBtn 按钮组  [sp1,sp2,sp3..]
		 * @param click 点击时回调的方法,格式:function()或function(index:Number);
		 * 
		 */		
		public function ClickSwapShowSomeBtnOne(arrBtn:Array,click:Function)
		{
			this.arrBtn=arrBtn;
			arrBtnIsMcToBtn=false;
			currentIndex=0;
			if(arrBtn[currentIndex] is McToBtn) arrBtnIsMcToBtn=true;
			this.click=click;
			allHide()
			setItemVisible(arrBtn[currentIndex],true);
		}
		private function click_fun(e:MouseEvent):void{
			setItemVisible(arrBtn[currentIndex],false);
			currentIndex++;
			if(currentIndex>this.arrBtn.length-1) currentIndex=0;
			setItemVisible(arrBtn[currentIndex],true);
			try{
				click(currentIndex);
			}catch(e:ArgumentError){
				click();
			}
		}
		private function setItemVisible(sp:Sprite,isOk:Boolean):void{
			if(arrBtnIsMcToBtn){
				var mcbtn:McToBtn=sp as McToBtn;
				mcbtn.getTarget().visible=isOk;
			}else{
				sp.visible=isOk;
			}
		}
		private function getItemVisible(sp:Sprite):Boolean{
			if(arrBtnIsMcToBtn){
				var mcbtn:McToBtn=sp as McToBtn;
				return mcbtn.getTarget().visible;
			}else{
				return sp.visible;
			}
		}
		/**
		 * 当前显示的第几个按钮 
		 * @return 
		 * 
		 */		
		public function get currentBtnIdex():Number{
			return currentIndex;
		}
		public function set currentBtnIdex(index:Number):void{
			if(currentBtnIdex<0||currentBtnIdex>this.arrBtn.length-1) return;
			setItemVisible(arrBtn[currentIndex],false);
			currentIndex=index;
			setItemVisible(arrBtn[currentIndex],true);
		}
		public function set visible(isOk:Boolean):void{
			setItemVisible(arrBtn[currentIndex],isOk);
		}
		public function get visible():Boolean{
			return getItemVisible(arrBtn[currentIndex]);
		}
		/**
		 * 所有隐藏
		 * 
		 */		
		protected function allHide():void{
			if(this.arrBtnIsMcToBtn){
				for(var i:Number=0;i<arrBtn.length;i++){
					var btnTem:McToBtn=arrBtn[i] as McToBtn;
					btnTem.getTarget().visible=false;
					btnTem.getTarget().addEventListener(MouseEvent.CLICK,click_fun);
				}
			}else{
				for(var j:Number=0;j<arrBtn.length;j++){
					var btnTem1:Sprite=arrBtn[j] as Sprite;
					btnTem1.addEventListener(MouseEvent.CLICK,click_fun);
					btnTem1.visible=false;
					
				}
			}
			
		}
	}
}